package org.chengn.level_middle.context1410;

import java.util.HashMap;
import java.util.Map;

public class Context1410 {
    private int val = 0;
    public String entityParser(String text) {
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < text.length(); ++i) {
            char c = text.charAt(i);
            if (c == '&') {
                char x = decode(text, i);
                if (x != ' ') {
                    c = x;
                    i += val - 1;
                }
            }
            str.append(c);

        }
        return str.toString();
    }

    private char decode(String key, int idx) {
        Map<String, Character> map = new HashMap<>();
        map.put("&quot;", '\"');
        map.put("&apos;", '\'');
        map.put("&amp;", '&');
        map.put("&gt;", '>');
        map.put("&lt;", '<');
        map.put("&frasl;", '/');

        for (int len = 4; idx + len <= key.length() && len <= 7; ++len) {
            String str2 = key.substring(idx, idx + len);
            if (map.containsKey(str2)) {
                val = len;
                return map.get(str2);
            }
        }
        return ' ';
    }
}
